package handlers

import (
	"gin-chat-server/common"
	"gin-chat-server/dto"
	service "gin-chat-server/services"
	"net/http"

	"github.com/gin-gonic/gin"
)

type AuthHandler struct {
	authService *service.AuthService
}

func NewAuthHandler() *AuthHandler {
	return &AuthHandler{
		authService: service.NewAuthService(),
	}
}

func (h *AuthHandler) Login(c *gin.Context) {
	var loginParams dto.JackpotUserLoginDTO
	if err := c.ShouldBindJSON(&loginParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	var result map[string]interface{}
	var err error
	if result, err = h.authService.Login(loginParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	c.Set("already_sent", true)
	common.Success(result).Send(c)

}

func (h *AuthHandler) Register(c *gin.Context) {
	var register dto.JackpotUserLoginDTO
	if err := c.ShouldBindJSON(&register); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	if _, err := h.authService.Register(register); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	c.Set("already_sent", true)
	common.Success("注册成功").Send(c)

}
